﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace cinetoona
{
    public partial class CreateNewMovie : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void CreateMovieBt_Click(object sender, EventArgs e)
        {
            try
            {
                DataAccessDataContext cinetoonadb = new DataAccessDataContext();
                Movies movies = new Movies();
                movies.Title = TitleTextBox.Text;
                movies.Release_Date = Convert.ToDateTime(LaunchDateTextBox.Text);
                movies.Price = Convert.ToInt32(PriceTextBox.Text);
                movies.Descript = DescriptionTextBox.Text;
                movies.Runtime = Convert.ToInt32(LengthTextBox.Text);
                movies.Resume = ResumeTextbox.Text;

                cinetoonadb.Movies.InsertOnSubmit(movies);
                cinetoonadb.SubmitChanges();
                GridView1.DataBind();
               
               
            }
            catch (Exception g)
            {
                DescriptionTextBox.Text = "Error no changes were made";
                Console.WriteLine(g.Message); //for troubleshooting purposes (EX. database server not available, etc.)
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataAccessDataContext cinetoonadb = new DataAccessDataContext();
            GridViewRow row = GridView1.SelectedRow;
            Movies containerMovie = new Movies();
            

            var q = from movies in cinetoonadb.Movies
                    where movies.Title == row.Cells[1].Text
                    select movies;

            foreach (var movies in q)
                containerMovie = movies;

            string date = String.Format("{0:dd/MM/yyyy}", containerMovie.Release_Date);
            TextBoxDescrip.Text = containerMovie.Descript;
            TitleTextBox.Text = containerMovie.Title;
            LaunchDateTextBox.Text = date;
            LengthTextBox.Text = Convert.ToString(containerMovie.Runtime);
            PriceTextBox.Text = Convert.ToString(containerMovie.Price);
            DescriptionTextBox.Text = containerMovie.Descript;
            ResumeTextbox.Text = containerMovie.Resume;
            selectedIdLable.Text = Convert.ToString(containerMovie.MovieID);
            

            
            

        }

        protected void UpdateMovieBt_Click(object sender, EventArgs e)
        {
            DataAccessDataContext cinetoonadb = new DataAccessDataContext();
            try
            {
                var q = (from movies in cinetoonadb.Movies
                        where movies.MovieID == Convert.ToInt32(selectedIdLable.Text)
                        select movies).Single();

                q.Descript = DescriptionTextBox.Text;
                q.Price = Convert.ToInt32(PriceTextBox.Text);
                q.Release_Date = Convert.ToDateTime(LaunchDateTextBox.Text);
                q.Runtime = Convert.ToInt32(LengthTextBox.Text);
                q.Resume = ResumeTextbox.Text;
                q.Title = TitleTextBox.Text;
                
                
                cinetoonadb.SubmitChanges();
                GridView1.DataBind();

            }
            catch (Exception g)
            {
                DescriptionTextBox.Text = "Error no changes were made";

            } 
        }

       
        protected void DeleteMovieBt_Click(object sender, EventArgs e)
        {
            DataAccessDataContext cinetoonadb = new DataAccessDataContext();
            try
            {
                var q = from movies in cinetoonadb.Movies
                        where movies.MovieID == Convert.ToInt32(selectedIdLable.Text)
                        select movies;

                foreach(var movies in q) 
                cinetoonadb.Movies.DeleteOnSubmit(movies);
                cinetoonadb.SubmitChanges();
                GridView1.DataBind();

            }
            catch(Exception g)
            {
                bool constraint = false;
                var q = from shows in cinetoonadb.Shows
                        where shows.MovieID == Convert.ToInt32(selectedIdLable.Text)
                        select shows;
                foreach(var shows in q)

                if (shows.MovieID == Convert.ToInt32(selectedIdLable.Text))
                {
                    constraint = true;
                }
                
                if(constraint == true)
                {
                 foreach (var shows in q)

                 DescriptionTextBox.Text = "Kan ikke slette filmen da en forestillingen med ID: " + shows.ShowID   +" bruger den";
                }
                else
                 DescriptionTextBox.Text = "Error no changes were made";
            }

        }

        
    }
}
